﻿using Framework;

namespace Game
{
    public class ScenePlayerBehaviorComponent : IComponent
    {
        private ScenePlayerBTContext _context;
        private IBehaviorTree _behaviorTree;
        
        public ScenePlayerBehaviorComponent()
        {

        }
        
        public void Dispose()
        {
            if (_behaviorTree != null)
            {
                BTreeScheduler.Instance.StopTree(_behaviorTree);
            }
        }

        public void Setup(ScenePlayerEntity scenePlayer)
        {
            _context = new ScenePlayerBTContext();
            _context.sceneObject = scenePlayer;
            BTreeBuilder<SceneObjectBTContext> builder = new BTreeBuilder<SceneObjectBTContext>(_context);
            SceneObjectStateComposite stateComposite = new SceneObjectStateComposite();
            builder.BeginCustomComposite(stateComposite);
            builder.Actionable(new SceneObjectIdleActionable());
            builder.Actionable(new ScenePlayerTrackActionable());
            builder.EndComposite();

            stateComposite.AddTransition(
                new SceneObjectDirectTransition(-1, ScenePlayerStates.Idle, 0));
            stateComposite.AddTransition(
                new ScenePlayerTrackObjectMoveTransition(-1, ScenePlayerStates.Track, 1));
            _behaviorTree = builder.Build();
            BTreeScheduler.Instance.StartTree(_behaviorTree); 
        }

        public void SetTrackObject(SceneObjectEntity sceneObject)
        {
            _context.trackObject = sceneObject;
        }
    }
}